package com.aier.proxy.entity.mapper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.RowMapper;

import com.aier.proxy.entity.ProxySub;

public class ProxySubRowMapper implements RowMapper<ProxySub> {

    @SuppressWarnings("unused")
    private int type;

    public ProxySubRowMapper() {
    }

    public ProxySubRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ProxySub mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseProxySub(new ProxySub(), rs);
    }

    public static ProxySub parseProxySub(ProxySub ps, ResultSet rs) throws SQLException {
        return parseUser(ps, rs, null);
    }

    public static ProxySub parseUser(ProxySub ps, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            ps.setPid(rs.getInt(su+"pid"));
        } catch (SQLException e) {
            ps.setPid(null);
        }
        try {
            ps.setSubPid(rs.getInt(su+"sub_pid"));
        } catch (SQLException e) {
            ps.setSubPid(null);
        }

        return ps;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, ProxySub ps) {
        par.put("pid",ps.getPid());
        par.put("sub_pid",ps.getSubPid());
    
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ProxySub ps) {
        args.put("pid",ps.getPid());
        args.put("sub_pid",ps.getSubPid());
        return args;
    }
    
    @SuppressWarnings("unused")
    private static String parseSQL(ProxySub p) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into proxy_sub(");
        int count = 0;
        if (null != p.getPid()) {
            sql.append("pid,");
            count++;
        }        
        if (null != p.getSubPid()) {
            sql.append("sub_pid,");
            count++;
        }


        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    @SuppressWarnings("unused")
    private static void parsePS(PreparedStatement ps, ProxySub p)
        throws SQLException {
        int count = 1;
        if (null != p.getPid()) {
            ps.setInt(count++, p.getPid());
        }
        if (null != p.getSubPid()) {
            ps.setInt(count++, p.getSubPid());
        }
    }


}
